/**
 * 获取开始时间和结束时间中间的每一天的倒排
 * @param {string} startTime
 * @param {string} endTime
 * @returns {Array}
 */
interface FormatTimeOption {
  (time: number | string, option?: string): string | null;
}
interface FormatNumber {
  (n: number): string;
}

const formatNumber: FormatNumber = function (n) {
  const str = n.toString();
  return str[1] ? str : `0${str}`;
};
interface ParseTime {
  (time: number | string | Date, cFormat?: string): string | null;
}

const parseTime: ParseTime = function (time, cFormat) {
  if (arguments.length === 0 || !time) {
    return null;
  }
  const format = cFormat || '{y}-{m}-{d} {h}:{i}:{s}';
  let date: Date;
  if (typeof time === 'object') {
    date = time;
  } else {
    if (typeof time === 'string') {
      if (/^[0-9]+$/.test(time)) {
        // support "1548221490638"
        time = parseInt(time);
      } else {
        // support safari
        // https://stackoverflow.com/questions/4310953/invalid-date-in-safari
        time = time.replace(new RegExp(/-/gm), '/');
      }
    }

    if (typeof time === 'number' && time.toString().length === 10) {
      time = time * 1000;
    }
    date = new Date(time);
  }
  const formatObj: { [key: string]: number } = {
    y: date.getFullYear(),
    m: date.getMonth() + 1,
    d: date.getDate(),
    h: date.getHours(),
    i: date.getMinutes(),
    s: date.getSeconds(),
    a: date.getDay(),
  };
  const time_str = format.replace(/{([ymdhisa])+}/g, (result, key) => {
    const value = formatObj[key];
    // Note: getDay() returns 0 on Sunday
    if (key === 'a') {
      return ['日', '一', '二', '三', '四', '五', '六'][value];
    }
    return value.toString().padStart(2, '0');
  });
  return time_str;
};

const formatTime: FormatTimeOption = function (time, option) {
  if (('' + time).length === 10) {
    time = typeof time === 'string' ? parseInt(time) * 1000 : time;
  } else {
    time = +time;
  }
  const d = new Date(time);
  // const now = Date.now();

  // const diff = (now - d) / 1000

  // if (diff < 30) {
  //   return '刚刚'
  // } else if (diff < 3600) {
  //   // less 1 hour
  //   return Math.ceil(diff / 60) + '分钟前'
  // } else if (diff < 3600 * 24) {
  //   return Math.ceil(diff / 3600) + '小时前'
  // } else if (diff < 3600 * 24 * 2) {
  //   return '1天前'
  // }
  if (option) {
    return parseTime(time, option);
  } else {
    return (
      d.getMonth() +
      1 +
      '月' +
      d.getDate() +
      '日' +
      d.getHours() +
      '时' +
      d.getMinutes() +
      '分'
    );
  }
};
interface GetDaysInTimeRange {
  (startTime: string, endTime: string): string[];
}

interface GetDate {
  (str: string): Date;
}

interface SortTime {
  (a: string, b: string): number;
}

const getDaysInTimeRange: GetDaysInTimeRange = function (startTime, endTime) {
  startTime = formatTime(new Date(startTime).getTime(), '{y}-{m}-{d}') || '';
  endTime = formatTime(new Date(endTime).getTime(), '{y}-{m}-{d}') || '';
  if (startTime === endTime) {
    return [startTime];
  }
  const getDate: GetDate = function (str) {
    const tempDate = new Date();
    const list = str.split('-');
    tempDate.setFullYear(parseInt(list[0]));
    tempDate.setMonth(parseInt(list[1]) - 1);
    tempDate.setDate(parseInt(list[2]));
    return tempDate;
  };
  let date1 = getDate(startTime);
  let date2 = getDate(endTime);
  if (date1 > date2) {
    const tempDate = date1;
    date1 = date2;
    date2 = tempDate;
  }
  date1.setDate(date1.getDate() + 1);
  const dateArr: string[] = [];
  let i = 0;
  while (
    !(
      date1.getFullYear() === date2.getFullYear() &&
      date1.getMonth() === date2.getMonth() &&
      date1.getDate() === date2.getDate()
    )
  ) {
    let dayStr = date1.getDate().toString();
    if (dayStr.length === 1) {
      dayStr = '0' + dayStr;
    }
    dateArr[i] =
      date1.getFullYear() +
      '-' +
      formatNumber(date1.getMonth() + 1) +
      '-' +
      dayStr;
    i++;
    date1.setDate(date1.getDate() + 1);
  }
  dateArr.splice(0, 0, startTime);
  dateArr.push(endTime);
  const sortTime: SortTime = function (a, b) {
    return new Date(b).getTime() - new Date(a).getTime();
  };
  dateArr.sort(sortTime);
  return dateArr;
};
